<?php

namespace App\Core;

class Response
{
    public static function success($data = null, string $message = '操作成功', int $code = 200): void
    {
        http_response_code(200);
        echo json_encode([
            'code' => $code,
            'message' => $message,
            'data' => $data
        ], JSON_UNESCAPED_UNICODE);
        exit;
    }

    public static function error(string $message = '操作失败', int $code = 400): void
    {
        http_response_code($code >= 500 ? $code : 200);
        echo json_encode([
            'code' => $code,
            'message' => $message,
            'data' => null
        ], JSON_UNESCAPED_UNICODE);
        exit;
    }

    public static function json($data, int $httpCode = 200): void
    {
        http_response_code($httpCode);
        echo json_encode($data, JSON_UNESCAPED_UNICODE);
        exit;
    }

    public static function paginate(array $list, int $total, int $page, int $pageSize): void
    {
        self::success([
            'list' => $list,
            'total' => $total,
            'page' => $page,
            'pageSize' => $pageSize,
            'totalPages' => ceil($total / $pageSize)
        ]);
    }
}

